/**
 * Created by meiguizhinian on 2018/7/4
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//单例/全静态/枚举
ivy.ControlManager = {
	getInstance: function () {
		return this;
	},

	//设置摇杆需要管理数量
	initJoyControlCount : function(count) {
		this._joyControlDataVec = [];
		for (let i = 0; i < count; ++i) {
			this._joyControlDataVec[i] = new ivy.ControlManager.JoyControlData();
		}
	},
	//设置按键需要管理数量
	initButtonControlCount : function(count) {
		this._buttonControlDataVec = [];
		for (let i = 0; i < count; ++i) {
			this._buttonControlDataVec[i] = new ivy.ControlManager.ButtonControlData();
		}
	},

	getJoyCount : function() {
		return this._joyControlDataVec.length;
	},

	getButtonCount : function() {
		return this._buttonControlDataVec.length;
	},

	setJoyState : function(controlIndex, state, isInState, degree=0, strength=0) {
		if (!this._activate) {
			return;
		}
		if (this._joyControlDataVec.length > controlIndex) {
			switch (state) {
				case ivy.ControlManager.ControlState.eTouching: {
					this._joyControlDataVec[controlIndex].getStateMap()[state].set(isInState, degree, strength);
					break;
				}
				case ivy.ControlManager.ControlState.eTouchEnter:
				case ivy.ControlManager.ControlState.eTouchDouble:
				case ivy.ControlManager.ControlState.eTouchEnd: {
					let self = this;
					ivy.Tools.pushFunctionToCocosThread(function() {
						self._joyControlDataVec[controlIndex].getStateMap()[state].set(isInState, degree, strength);
						if (isInState) {
							ivy.Tools.pushFunctionToCocosThread(function() {
								self._joyControlDataVec[controlIndex].getStateMap()[state].set(false);
							});
						}
					});
					break;
				}
				default:
					break;
			}
		}
	},
	setButtonState : function(controlIndex, state, isInState) {
		if (!this._activate) {
			return;
		}
		if (this._buttonControlDataVec.length > controlIndex) {
			switch (state) {
				case ivy.ControlManager.ControlState.eTouching: {
					this._buttonControlDataVec[controlIndex].getStateMap()[state].set(isInState);
					if(isInState) {
						this._buttonControlDataVec[controlIndex].getStateMap()[ivy.ControlManager.ControlState.eTouchNone].set(false);
					}
					break;
				}
				case ivy.ControlManager.ControlState.eTouchEnter:
				case ivy.ControlManager.ControlState.eTouchDouble:
				case ivy.ControlManager.ControlState.eTouchEnd: {
					let self = this;
					ivy.Tools.pushFunctionToCocosThread(function() {
						self._buttonControlDataVec[controlIndex].getStateMap()[state].set(isInState);
						if (isInState) {
							self._buttonControlDataVec[controlIndex].getStateMap()[ivy.ControlManager.ControlState.eTouchNone].set(false);
							ivy.Tools.pushFunctionToCocosThread(function() {
								if (state === ivy.ControlManager.ControlState.eTouchEnd) {
									self._buttonControlDataVec[controlIndex].getStateMap()[ivy.ControlManager.ControlState.eTouching].set(false);
									self._buttonControlDataVec[controlIndex].getStateMap()[ivy.ControlManager.ControlState.eTouchNone].set(true);
								}
								self._buttonControlDataVec[controlIndex].getStateMap()[state].set(false);
							});
						}
					});
					break;
				}
				default:
					break;
			}
		}
	},

	testJoyState : function(controlIndex, state) {
		return this._joyControlDataVec[controlIndex].getStateMap()[state].isInState;
	},
	testButtonState : function(controlIndex, state) {
		return this._buttonControlDataVec[controlIndex].getStateMap()[state].isInState;
	},

	getJoyState : function(controlIndex, state) {
		return this._joyControlDataVec[controlIndex].getStateMap()[state];
	},
	getButtonState : function(controlIndex, state) {
		return this._buttonControlDataVec[controlIndex].getStateMap()[state];
	},

	reset : function() {
		for (let i=0; i!==this._joyControlDataVec.length; ++i) {
			let data = this._joyControlDataVec[i];
			let stateMap = data.getStateMap();
			for (let stateKV in stateMap) {
				if (!stateMap.hasOwnProperty(stateKV)) {
					continue;
				}
				stateMap[stateKV].isInState = (stateKV === ivy.ControlManager.ControlState.eTouchNone);
			}
		}
		for (let i=0; i!==this._buttonControlDataVec.length; ++i) {
			let data = this._buttonControlDataVec[i];
			let stateMap = data.getStateMap();
			for (let stateKV in stateMap) {
				if (!stateMap.hasOwnProperty(stateKV)) {
					continue;
				}
				stateMap[stateKV].isInState = (stateKV === ivy.ControlManager.ControlState.eTouchNone);
			}
		}
	},
	setActivate : function(act) {
		this._activate = act;
		if (!act) {
			this.reset();
		}
	},
	isActivate : function() {
		return this._activate;
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////private:
	_activate : true,
	_joyControlDataVec : [],//std::vector<JoyControlData>
	_buttonControlDataVec : [],//std::vector<ButtonControlData>
};

ivy.ControlManager.ControlState = {
	eTouchDouble    : 0,
	eTouchEnter     : 1,
	eTouchEnd       : 2,
	eTouching       : 3,
	eTouchNone      : 4,
	eEnumCount      : 5
};

//对象 let obj = new ll.Obj(param)
(function(){
	ivy.ControlManager.JoyBaseState = function (param) {
		//成员变量
		this.isInState = false;
		this.joyDegree = 0;
		this.joyStrength = 0;
	};
	let p = ivy.ControlManager.JoyBaseState.prototype;

	p.set = function (isInState, degree=0, strength=0) {
		this.isInState = isInState;
		this.joyDegree = degree;
		this.joyStrength = strength;
	};
})();

(function(){
	ivy.ControlManager.ButtonBaseState = function (param) {
		//成员变量
		this.isInState = false;
	};
	let p = ivy.ControlManager.ButtonBaseState.prototype;

	p.set = function (isInState) {
		this.isInState = isInState;
	};
})();

(function(){
	ivy.ControlManager.JoyControlData = function (param) {
		//成员变量
		this._stateMap = {};
		this._stateMap[ivy.ControlManager.ControlState.eTouchDouble] = new ivy.ControlManager.JoyBaseState();
		this._stateMap[ivy.ControlManager.ControlState.eTouchEnter] = new ivy.ControlManager.JoyBaseState();
		this._stateMap[ivy.ControlManager.ControlState.eTouchEnd] = new ivy.ControlManager.JoyBaseState();
		this._stateMap[ivy.ControlManager.ControlState.eTouching] = new ivy.ControlManager.JoyBaseState();
		this._stateMap[ivy.ControlManager.ControlState.eTouchNone] = new ivy.ControlManager.JoyBaseState();
		this._stateMap[ivy.ControlManager.ControlState.eTouchNone].set(true);
	};
	let p = ivy.ControlManager.JoyControlData.prototype;

	p.getStateMap = function () {
		return this._stateMap;
	};
})();

(function(){
	ivy.ControlManager.ButtonControlData = function (param) {
		//成员变量
		this._stateMap = {};
		this._stateMap[ivy.ControlManager.ControlState.eTouchDouble] = new ivy.ControlManager.ButtonBaseState();
		this._stateMap[ivy.ControlManager.ControlState.eTouchEnter] = new ivy.ControlManager.ButtonBaseState();
		this._stateMap[ivy.ControlManager.ControlState.eTouchEnd] = new ivy.ControlManager.ButtonBaseState();
		this._stateMap[ivy.ControlManager.ControlState.eTouching] = new ivy.ControlManager.ButtonBaseState();
		this._stateMap[ivy.ControlManager.ControlState.eTouchNone] = new ivy.ControlManager.ButtonBaseState();
		this._stateMap[ivy.ControlManager.ControlState.eTouchNone].set(true);
	};
	let p = ivy.ControlManager.ButtonControlData.prototype;

	p.getStateMap = function () {
		return this._stateMap;
	};
})();